import { Injectable, Optional, Inject } from '@nestjs/common';
import { IHttpClient } from './interface';

@Injectable()
export class AppService {
    // 依赖项被标记为可选
    constructor(
        @Optional()
        @Inject('HTTP_OPTIONS')
        private readonly httpClient?: IHttpClient
    ) { }

    async fetchData() {
        if (this.httpClient) {
            // 使用提供的自定义 HTTP 客户端
            return this.httpClient.get('https://api.example.com/data');
        } else {
            // 回退到默认实现
            return this.defaultFetch();
        }
    }

    private async defaultFetch() {
        // 这里是默认的 HTTP 实现
        return {
            data: 'Default response',
            message: 'Using built-in HTTP client'
        };
    }
}